package com.gitee.secretopen.sso.controller;


import cn.chenc.framework.core.annotation.BussinessLog;
import cn.chenc.framework.core.model.request.RequestHolder;
import cn.chenc.framework.core.model.response.ResponseVO;
import cn.chenc.framework.core.model.response.ResultUtil;
import cn.chenc.framework.core.util.IDUtils;
import cn.chenc.framework.excel.util.EasyExcelUtil;
import com.gitee.secretopen.sso.controller.form.PageInfo;
import com.gitee.secretopen.sso.db.pojo.Site;
import com.gitee.secretopen.sso.excelmodel.SiteUpload;
import com.gitee.secretopen.sso.service.SiteService;
import com.google.common.primitives.Ints;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 应用表 前端控制器
 * </p>
 *
 * @author secret
 * @since 2021-02-25
 */
@RestController
@RequestMapping("/admin")
public class SiteController {
    @Autowired
    private SiteService siteService;

    @GetMapping("/site/{id}")
    public ResponseVO<Site> getOne(@PathVariable("id") int id) {
        Site site = siteService.getById(id);
        return ResultUtil.success(site);
    }

    @PostMapping("/sites")
    @BussinessLog("查询应用列表")
    public ResponseVO<PageInfo<Site>> list(@RequestBody Site params){
        PageInfo<Site> page = siteService.page(params);
        return ResultUtil.success(page);
    }

    @PostMapping("/site/getByName")
    public ResponseVO<PageInfo<Site>> getByName(@RequestBody Site site) {
        PageInfo<Site> page = siteService.getByName(site);
        return ResultUtil.success(page);
    }

    @PostMapping("/site")
    @BussinessLog("添加应用")
    public ResponseVO save(@RequestBody Site site){
        siteService.addSite(site);
        return ResultUtil.success("新增成功！");
    }

    @PutMapping("/site")
    @BussinessLog("修改应用")
    public ResponseVO update(@RequestBody Site site) {
        siteService.updateById(site);
        return ResultUtil.success("更新成功！");
    }

    @DeleteMapping("/site/{id}")
    public ResponseVO delete(@PathVariable("id") int id) {
        siteService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    @DeleteMapping("/sites")
    public ResponseVO deleteMulti(@RequestBody int[] ids) {
        List<Integer> id = Ints.asList(ids);
        siteService.removeByIds(id);
        return ResultUtil.success("删除成功");
    }

    @ResponseBody
    @PostMapping(value = "/sites/download")
    @BussinessLog("应用列表导出")
    public void downloadSites() {
        List<Site> siteList = siteService.downloadSites();
        HttpServletResponse response = RequestHolder.getResponse();
        EasyExcelUtil.toExcle(response, siteList, "secret_sso_应用清单", SiteUpload.class);
    }

    @GetMapping("/siteAdd")
    public ResponseVO insert() {
        Site site = new Site();
        site.setSiteId("secret_sso_"+IDUtils.genItemId());
        site.setSiteName("test_"+IDUtils.genItemId());
        site.setCreateTime(LocalDateTime.now());
        site.setCreateBy("root");
        site.setUpdateTime(LocalDateTime.now());
        site.setUpdateBy("root");
        site.setIsDelete(0);
        boolean result = siteService.save(site);
        if (result) {
            return ResultUtil.success("成功！");
        }
        return ResultUtil.error("失败！");
    }

    @ResponseBody
    @PostMapping(value = "/site/upload")
    @BussinessLog("批量导入应用")
    public ResponseVO upload(MultipartFile file) throws Exception{
        return siteService.upload(file);
    }

}

